package oel;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

public class sendSocket {
    private static Socket photoshopSocket = null;
    public static Socket connectPs(){
        try {
            photoshopSocket = new Socket("0.0.0.0", 49494);
        }catch (Exception e){e.printStackTrace();}
        return photoshopSocket;
    }

    public static String send(String message){
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;
        try {
            Protocol Protocol = new Protocol("123456");
            outputStream = new DataOutputStream(photoshopSocket.getOutputStream());
            inputStream = new DataInputStream(photoshopSocket.getInputStream());
            byte[] bytesStr = Protocol.processJavaScript(message, outputStream, inputStream);
            byte[] newBytesStr = new byte[bytesStr.length];
            int j = 0;
            for (int i = 0; i < bytesStr.length; i++) {
                if (bytesStr[i] != 13) {
                    newBytesStr[j++] = bytesStr[i];
                } else {
                    newBytesStr[j++] = ' ';
                }
            }
            String returnString = new String(newBytesStr);
            outputStream.close();
            inputStream.close();
            photoshopSocket.close();
            return returnString;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
